<?php

namespace tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    /**
     * 基础URL
     */
    protected string $baseUrl = 'http://localhost:8000';

    /**
     * 测试用户名
     */
    protected string $username = 'liuhui';

    /**
     * 测试密码
     */
    protected string $password = 'zhilv2023';

    /**
     * API Token
     */
    protected ?string $token = null;

    /**
     * 登录用户信息
     */
    protected ?array $userInfo = null;

    /**
     * 设置测试环境
     */
    protected function setUp(): void
    {
        parent::setUp();
        
        // 可以在这里添加全局的测试设置
    }

    /**
     * 清理测试环境
     */
    protected function tearDown(): void
    {
        // 可以在这里添加全局的测试清理
        
        parent::tearDown();
    }

    /**
     * 发送GET请求
     */
    protected function get(string $uri, array $headers = []): array
    {
        return $this->request('GET', $uri, [], $headers);
    }

    /**
     * 发送POST请求
     */
    protected function post(string $uri, array $data = [], array $headers = []): array
    {
        return $this->request('POST', $uri, $data, $headers);
    }

    /**
     * 发送PUT请求
     */
    protected function put(string $uri, array $data = [], array $headers = []): array
    {
        return $this->request('PUT', $uri, $data, $headers);
    }

    /**
     * 发送DELETE请求
     */
    protected function delete(string $uri, array $data = [], array $headers = []): array
    {
        return $this->request('DELETE', $uri, $data, $headers);
    }

    /**
     * 发送HTTP请求
     */
    protected function request(string $method, string $uri, array $data = [], array $headers = []): array
    {
        $url = $this->baseUrl . $uri;
        
        // 添加认证头
        if ($this->token) {
            $headers['Authorization'] = 'Bearer ' . $this->token;
        }
        
        // 添加内容类型头
        if (!isset($headers['Content-Type']) && in_array($method, ['POST', 'PUT', 'PATCH'])) {
            $headers['Content-Type'] = 'application/json';
        }

        $curl = curl_init();
        
        curl_setopt_array($curl, [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST => $method,
            CURLOPT_HTTPHEADER => $this->buildHeaders($headers),
        ]);
        
        if (!empty($data) && in_array($method, ['POST', 'PUT', 'PATCH'])) {
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
        }
        
        $response = curl_exec($curl);
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $error = curl_error($curl);
        
        curl_close($curl);
        
        if ($error) {
            throw new \Exception("cURL Error: " . $error);
        }
        
        $result = json_decode($response, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \Exception("JSON decode error: " . json_last_error_msg());
        }
        
        return [
            'status_code' => $httpCode,
            'data' => $result
        ];
    }

    /**
     * 构建HTTP头
     */
    protected function buildHeaders(array $headers): array
    {
        $result = [];
        foreach ($headers as $key => $value) {
            $result[] = $key . ': ' . $value;
        }
        return $result;
    }

    /**
     * 用户登录并获取token
     */
    protected function login(): bool
    {
        $response = $this->post('/api/auth/login', [
            'username' => $this->username,
            'password' => $this->password
        ]);

        if ($response['status_code'] === 200 && $response['data']['code'] === 0) {
            $this->token = $response['data']['data']['token'];
            $this->userInfo = $response['data']['data']['userInfo'];
            return true;
        }

        return false;
    }
}